﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Input;

namespace Spacer
{
    public class ConnectionsMenu : Activity
    {
        private AvailableNetworkSessionCollection Connectables;
        private int CurrentPage;
        private int Selection;
        private const int CONNECTIONS_PER_PAGE = 20;

        public void Initialize()
        {
            Connectables = Network.FindSessions();
            CurrentPage = 1;
            Selection = 0;
        }

        public void Paused()
        {

        }

        public void Resumed()
        {

        }

        public void Shutdown()
        {
        }

        public void Update()
        {
            if ((Input.IsKeyDown(Keys.Space) && !Input.WasKeyDown(Keys.Space)) ||
                (Input.IsGamePadButtonDown(GamePadButton.B) && !Input.WasGamePadButtonDown(GamePadButton.B)))
                Finished = true;

            UpdateConnectionsMenu();
        }

        public void DrawWorld()
        {
        }

        public void DrawOverlay()
        {
            //Draw Black Background, or BLACKGROUND!
            Surface.DrawColor = Color.Black;
            Surface.DrawRect(Vector2.Zero, new Vector2(Surface.Width, Surface.Height), Vector2.Zero, 0f);

            Surface.DrawColor = Color.Yellow;
            if (Connectables == null)
            {
                Surface.DrawString("Default", "No available connections, press 'B' to exit", new Vector2(100, 100), Vector2.Zero, 0f);
            }
            else
            {
                //Draw the Connections
                int count = 0;
                for (int i = (CurrentPage - 1 * CONNECTIONS_PER_PAGE); (i < Connectables.Count) && (i < CurrentPage * CONNECTIONS_PER_PAGE); i++)
                {
                    if (count == Selection)
                        Surface.DrawColor = Color.LightGreen;
                    Surface.DrawString("Default", Connectables[i].HostGamertag + "'s Galaxy", new Vector2(0, 20 * count), Vector2.Zero, 0f);
                    if (count == Selection)
                        Surface.DrawColor = Color.Yellow;
                    count++;
                }
            }
        }

        public bool IsModal()
        {
            return true;
        }

        public bool IsTransparent()
        {
            return true;
        }

        public bool IsFinished()
        {
            return Finished;
        }

        private void UpdateConnectionsMenu()
        {
            //Scroll through connections
            if ((Input.IsKeyDown(Keys.S) && !Input.WasKeyDown(Keys.S)) ||
                (Input.IsGamePadButtonDown(GamePadButton.Down) && !Input.WasGamePadButtonDown(GamePadButton.Down)))
            {
                Selection++;
                if (Selection >= CONNECTIONS_PER_PAGE)
                    Selection = 0;
            }
            else if ((Input.IsKeyDown(Keys.W) && !Input.WasKeyDown(Keys.W)) ||
                     (Input.IsGamePadButtonDown(GamePadButton.Up) && !Input.WasGamePadButtonDown(GamePadButton.Up)))
            {
                Selection--;
                if (Selection < 0)
                    Selection = CONNECTIONS_PER_PAGE - 1;
            }

            //make sure there are galaxies
            if (Connectables != null)
            {
                //Change displayed connections to next page
                if (CurrentPage > 1 && (Input.IsGamePadButtonDown(GamePadButton.LeftBumper) && !Input.WasGamePadButtonDown(GamePadButton.LeftBumper)))
                    CurrentPage--;
                else if ((CurrentPage * CONNECTIONS_PER_PAGE < Connectables.Count) && (Input.IsGamePadButtonDown(GamePadButton.RightBumper) && !Input.WasGamePadButtonDown(GamePadButton.RightBumper)))
                    CurrentPage++;

                //Select a connection
                if ((Input.IsKeyDown(Keys.Enter) && !Input.WasKeyDown(Keys.Enter)) ||
                    (Input.IsGamePadButtonDown(GamePadButton.A) && !Input.WasGamePadButtonDown(GamePadButton.A)))
                {
                    AvailableNetworkSession SelectedConnection = Connectables[Selection + ((CurrentPage - 1) * CONNECTIONS_PER_PAGE)];
                    Network.JoinSession(SelectedConnection);
                    Finished = true;
                }
            }
        }

        // ----------

        bool Finished { get; set; }
    }
}
